//
//  YWProfileCustomView.m
//  maintain
//
//  Created by Kyo on 16/2/19.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWProfileCustomView.h"
#import "YWMacro.h"
#import "UIView+Extension.h"
#import "UIImage+Extension.h"
#import "UIColor+CustomColors.h"
#import "YWFunctionModel.h"

@interface YWProfileCustomView () {
    
}
@property (nonatomic, strong) UILabel* redCountLabel;
@property (nonatomic, strong) UIButton* itmeButton;
@end

@implementation YWProfileCustomView

-(UILabel *)redCountLabel{
    if (!_redCountLabel) {
        UILabel *redCountLabel = [[UILabel alloc]init];
        redCountLabel.font = kContentFont;
        redCountLabel.backgroundColor = [UIColor redColor];
        redCountLabel.textAlignment = NSTextAlignmentCenter;
        redCountLabel.textColor = [UIColor whiteColor];
        redCountLabel.layer.masksToBounds = YES;
        [self addSubview:redCountLabel];
        _redCountLabel = redCountLabel;
    }
    return _redCountLabel;
}

-(UIButton *)itmeButton{
    if (!_itmeButton) {
        UIButton *itmeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [itmeButton setBackgroundColor:[UIColor whiteColor]];
        itmeButton.titleLabel.font = kContentFont;
        itmeButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [itmeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [itmeButton setBackgroundImage:[UIImage imageWithColor:[UIColor customLineColor]] forState:UIControlStateHighlighted];
        itmeButton.layer.masksToBounds = YES;
        [itmeButton addTarget:self action:@selector(clickButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:itmeButton];
        _itmeButton = itmeButton;
    }
    return _itmeButton;
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    
    [self.itmeButton setFrame:CGRectMake(0, 0, self.width, self.height)];

    self.itmeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.itmeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    CGFloat imageSize = 32;
    
    CGFloat heightGap = (self.height-imageSize-self.buttonLabelSize.height)/3;
    self.itmeButton.imageEdgeInsets = UIEdgeInsetsMake(heightGap,(self.width - imageSize)/2,0,0);
    self.itmeButton.titleEdgeInsets = UIEdgeInsetsMake(imageSize +heightGap*2,  (self.width - self.buttonLabelSize.width) / 2  -  imageSize, 0, 0);
    CGFloat x = (self.width/3)*2;
    CGFloat y = heightGap;
    CGFloat size = 20;
    [self.redCountLabel setFrame:CGRectMake(x, y, size, size)];
    self.redCountLabel.layer.cornerRadius = size/2;
}

- (void)setFunction:(YWFunctionModel *)function{
    _function = function;
    [self.itmeButton setImage:[UIImage imageNamed:function.imageString] forState:UIControlStateNormal];
    [self.itmeButton setTitle:function.functionName forState:UIControlStateNormal];
    if ([function.redCount integerValue]>0) {
        self.redCountLabel.text = function.redCount;
        self.redCountLabel.hidden = NO;
    }else{
        self.redCountLabel.hidden = YES;
    }
}

//-(void)setDataDic:(NSDictionary *)dataDic{
//    NSString *image = [dataDic objectForKey:KImage];
//    [self.itmeButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
//    NSString *name = [dataDic objectForKey:KName];
//    [self.itmeButton setTitle:name forState:UIControlStateNormal];
//    NSString *count = [dataDic objectForKey:KCount];
//    if ([count integerValue]>0) {
//        self.redCountLabel.text = count;
//        self.redCountLabel.hidden = NO;
//    }else{
//        self.redCountLabel.hidden = YES;
//    }
//    
//}

-(void)setNoRedCount:(NSString *)noRedCount{
    _noRedCount = noRedCount;
    if ([noRedCount integerValue]>0) {
        self.redCountLabel.text = noRedCount;
        self.redCountLabel.hidden = NO;
    }else{
        self.redCountLabel.hidden = YES;
    }
}

-(void)clickButtonEvent:(id)sender{
    NSInteger tag = self.tag - 200;
    if ([self.delegate respondsToSelector:@selector(clickItemButtonWithTag:)]) {
        [self.delegate clickItemButtonWithTag:tag];
    }
}

@end
